import { ref, unref } from 'vue'
export function useCustomRow(propsRef, tableElRef, selectable, { emit }) {
  const multipleSelection = ref([])
  const rowKey = unref(propsRef).rowKey
  const customRowClick = (row) => {
    if (!selectable(row)) return
    const tableRef = unref(tableElRef)
    if (multipleSelection.value.find((item) => item[rowKey] === row[rowKey])) {
      multipleSelection.value.splice(
        multipleSelection.value.findIndex((item) => {
          return item[rowKey] === row[rowKey]
        }),
        1
      )
      tableRef.toggleRowSelection(row, false)
    } else {
      multipleSelection.value.push(row)
      tableRef.toggleRowSelection(row, true)
    }

    emit('row-click', multipleSelection.value, tableElRef)
  }
  // 接受全选数据 到multipleSelection
  const custAllData = (rows) => {
    multipleSelection.value = rows
  }
  const clearSelected = () => {
    const tableRef = unref(tableElRef)
    tableRef.clearSelection()
    multipleSelection.value = []
  }
  return {
    customRowClick,
    custAllData,
    multipleSelection,
    clearSelected
  }
}
